package org.se.scut.pinterest.mina.handler;


import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.se.scut.pinterest.mina.codec.msg.Message;
import org.se.scut.pinterest.mina.codec.msg.PicturesGetMessage;
import org.se.scut.pinterest.mina.codec.msg.PicturesPostMessage;
import org.se.scut.pinterest.mina.handler.observer.MessageOperatorType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServerIOHandler extends IoHandlerAdapter {

	private static final Logger LOGGER = LoggerFactory.getLogger(ServerIOHandler.class);
	private static final AttributeKey CONTEXT_KEY = new AttributeKey(ServerIOHandler.class, "context");
	
	private final MessageHandlerProxy proxy;
	
	public ServerIOHandler()
	{
		proxy = new MessageHandlerProxy();
	}
	
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {

		LOGGER.info("server received message");
		LOGGER.info(message.toString());
		proxy.callObserver(session ,(Message)message, MessageOperatorType.CREATE);
		super.messageReceived(session, message);
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		
		LOGGER.info("server session created");
		super.sessionCreated(session);
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		
		LOGGER.info("server session opend");
		session.setAttribute("key", 1);
		//session.write("message from server! \r\n");
		super.sessionOpened(session);
	}

	
	
}
